#include "MiniCamera.h"

MiniCamera::MiniCamera(float distance, float pitch, float yaw)
    : m_moving(false), m_distance(distance), m_pitch(pitch), m_yaw(yaw), m_rate(0.01)
{
    m_worldUp = {0, 1, 0};
    UpdateCameraVectors();
}

QMatrix4x4 MiniCamera::GetViewMatrix() const
{
    QMatrix4x4 view;
    QVector3D position = m_front * m_distance;
    view.lookAt(position, {0, 0, 0}, m_up);
    return view;
}

QVector3D MiniCamera::GetPosition() const
{
    return m_front * m_distance;
}

bool MiniCamera::GetMoving() const
{
    return m_moving;
}

float MiniCamera::GetDistance() const
{
    return m_distance;
}

float MiniCamera::GetPitch() const
{
    return m_pitch;
}

float MiniCamera::GetYaw() const
{
    return m_yaw;
}

float MiniCamera::GetRate() const
{
    return m_rate;
}

void MiniCamera::SetMoving(bool moving)
{
    m_moving = moving;
}

void MiniCamera::SetDistance(float d)
{
    m_distance = d;
    UpdateCameraVectors();
}

void MiniCamera::SetPitch(float pitch)
{
    m_pitch = pitch;
    UpdateCameraVectors();
}

void MiniCamera::SetYaw(float yaw)
{
    m_yaw = yaw;
    UpdateCameraVectors();
}

void MiniCamera::UpdateCameraVectors()
{
    // 先算出方向，然后规范化
    QVector3D front;
    front.setX(cos(-m_yaw) * cos(m_pitch));
    front.setY(sin(m_pitch));
    front.setZ(sin(-m_yaw) * cos(m_pitch));
    m_front = front.normalized();

    // 然后叉乘计算另外两个方向
    m_right = QVector3D::crossProduct(m_front, m_worldUp).normalized();
    m_up = QVector3D::crossProduct(m_right, m_front).normalized();
}
